package com.insyte.fragments;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.insyte.R;
import com.insyte.config.Constants;
import com.insyte.model.User;
import com.insyte.utils.BitmapUtility;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class UserFragment extends Fragment {
    private ViewGroup view;
    private TextView tv_name, tv_dating_status, tv_age, tv_height;
    private ImageView img_avatar;
    private JSONObject js_user_marker;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    private User user_marker = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null && bundle.containsKey(Constants.KEY_USER_MARKER)) {
//            String user_marker_string = bundle.getString(Constants.KEY_USER_MARKER);
            user_marker = bundle.getParcelable(Constants.KEY_USER_MARKER);
/*            try {
                js_user_marker = new JSONObject(user_marker_string);
            } catch (JSONException e) {
                e.printStackTrace();
            }*/
        }
        creatOptionImg();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = (ViewGroup) inflater.inflate(R.layout.fragment_user, container, false);
        getViewInfoUser();
        /*if (js_user_marker != null) {
            setDataUser();
        }*/
        if (user_marker != null) {
            setDataUser();
        }
        return view;
    }

    private void getViewInfoUser() {
        tv_name = (TextView) view.findViewById(R.id.tv_name);
        tv_age = (TextView) view.findViewById(R.id.tv_age);
        tv_height = (TextView) view.findViewById(R.id.tv_height);
        tv_dating_status = (TextView) view.findViewById(R.id.tv_dating_status);
        img_avatar = (ImageView) view.findViewById(R.id.img_avatar);
    }

    private void setDataUser() {
        /*try {
            tv_name.setText(js_user_marker.getString("first_name") + " " + js_user_marker.getString("last_name"));
            tv_age.setText(String.valueOf(js_user_marker.getInt("age")));
            String url_img_user = Constants.URL_API + js_user_marker.getString("profile_picture");
            imageLoader.displayImage(url_img_user, img_avatar, options);
        } catch (JSONException e) {
            e.printStackTrace();
        }*/

            tv_name.setText(user_marker.getFirst_name() + " " + user_marker.getLast_name());
            tv_age.setText(user_marker.getAge());
            String url_img_user = Constants.URL_API + user_marker.getProfile_picture();
            imageLoader.displayImage(url_img_user, img_avatar, options);

    }


    private void creatOptionImg() {
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(false)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .build();
        new DisplayImageOptions.Builder().imageScaleType(ImageScaleType.EXACTLY);
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()));
    }
}
